/**
 * 基于ol、天地图为底图的
 * uniapp的OL操作方法封装
 */
import constants from '../constants';
import funs, {
	fLayer,
	fMap,
	fScript,
	fCommon,
	fView,
	fFeature,
	fCustomEvents,
	fStyle
} from '../functions';

import { fForEachFeatureAtPixel } from '../functions/fMap';

class DuoMapUni {
	constructor(configs) {
		this.configs = configs || constants.DEFALUTCONFIGS;
		this.beforeCreateEvents = [];
	}
	init(callback) {
		// 1. 创建脚本和样式文件
		fScript.fCreateDOM('link', this.configs.ol.files.cssPath);
		fScript.fCreateDOM('script', this.configs.ol.files.jsPath, () => {
			// 2. 初始化map
			fMap.fCreateOLMAP(this.configs, (mapInstance) => {
				//3. 加载TDT底图 (可选)
				if (this.configs.tdt) {
					//console.log(mapInstance);
					mapInstance.setLayers(fLayer.fInitTDTBaseMap(this.configs.ol.proj || 'EPSG:4326',
						this.configs.tdt));
				}
				this._mapInstance = mapInstance;
				// 事件绑定处理
				this.bindMapEvent();
				fCommon.isFun(callback) && callback(mapInstance) && this.callMapInstanceEvents();
			});
		});
		// 加载turf工具(如果有)
		this.configs.ol.files.turfPath && fScript.fCreateDOM('script', this.configs.ol.files.turfPath);
		return this;
	}
	// 绑定地图事件+自定义事件
	bindMapEvent() {
		this._mapInstance.on('click', fCustomEvents.fMapDbClickDispatchEvent);
		this._mapInstance.on('dbClick', fCustomEvents.fMapDbClickDispatchEvent);
	}
	// 获取地图实例
	getMapInstance() {
		return this._mapInstance;
	}
	// 操作map实例的事件
	handleMapInstanceEvent(callback, ...params) {
		if (this.getMapInstance()) {
			return callback && callback.apply(this._mapInstance, params);
		} else {
			this.beforeCreateEvents.push({ fn: callback, params })
		}
	}
	//
	// 调用这些map实例相关的事件
	callMapInstanceEvents() {
		this.beforeCreateEvents.forEach(item => {
			if (fCommon.isFun(item.fn)) {
				return item.fn(...[this._mapInstance, ...params])
			} else {
				return this;
			}
		});
		this.beforeCreateEvents.length > 0 && (this.beforeCreateEvents = []);
	}
	// 获取所有layers
	getLayers() {
		return this.handleMapInstanceEvent(fLayer.fGetLayers);
	}
	// 切换底图
	changeBaseMap(baseMapGroupIndex = 0) {
		this.handleMapInstanceEvent(fLayer.fChangeBaseMap, baseMapGroupIndex);
		return this;
	}
	// 动画移动到指定位置
	animateMoving(params) {
		this.handleMapInstanceEvent(fView.fAnimate, params);
		return this;
	}
	// 地图被单击
	onMapClick(callback) {
		fCustomEvents.fListenCustomEvent(constants.DEFAULTEVENTNAMES.MAPCLICK, (e) => {
			callback && callback(e.detail);
			fForEachFeatureAtPixel(e.detail);
		});
		return this;
	}
	// 地图被双击
	onMapDBClick(callback) {
		fCustomEvents.fListenCustomEvent(constants.DEFAULTEVENTNAMES.MAPDBCLICK, (e) => { callback && callback(e.detail) });
		return this
	}
	// 添加图层
	addLayer(layer) {
		this.handleMapInstanceEvent(fMap.fAddLayer, layer);
	}
	// 快速添加矢量图层
	addVL(...arg){
		this.addLayer(this.createVectorLayer(...arg));
	}
	// 创建矢量图层
	createVectorLayer(...arg) {
		return fLayer.fCreateVectorLayer(...arg);
	}
	// 创建要素
	createFeature(...arg) {
		return fFeature.fCreateFeature(...arg);
	}
	// 创建点要素
	createPointFeature(...arg) {
		return fFeature.fCreatePointFeature(...arg);
	}
	// 获取图层
	getLayer(props,val){
		return this.handleMapInstanceEvent(fLayer.fGetLayer,props,val)
	}
	// 通过id获取图层
	getLayerByID(id){
		return this.handleMapInstanceEvent(fLayer.fGetLayerByID,id);
	}
	useStyle() {
		return () => funs.fStyle;
	}
	useLayer() {
		return () => funs.fLayer;
	}
	useFeature() {
		return () => funs.fFeature;
	}
	useMap() {
		return () => fMap;
	}
	createStyle(...arg) {
		return fStyle.fcreateStyle(...arg);
	}
	createLineStyle(...arg) {
		return fStyle.fcreateLineStyle(...arg);
	}
	createCircleStyle(...arg){
		return fStyle.fcreateCircleStyle(...arg);
	}
	createIconStyle(...arg){
		return fStyle.fcreateIconStyle(...arg)
	}
	createStyleFn(callback=()=>{}){
		return (feature)=>{
			return callback(feature);
		}
	}
	setLayerProps(...arg) {
		return fLayer.fSetLayerProps(...arg);
	}
	getSize(){
		return this.handleMapInstanceEvent(fView.fGetSize);
	}
	getZoom(){
		return this.handleMapInstanceEvent(fView.fGetZoom);
	}
	getCenter(){
		return this.handleMapInstanceEvent(fView.fGetCenter);
	}
	setSize(){
		this.handleMapInstanceEvent(fView.fGetSize);
		return this;
	}
	setZoom(){
		this.handleMapInstanceEvent(fView.fGetZoom);
		return this;
	}
	setCenter(){
		this.handleMapInstanceEvent(fView.fGetCenter);
		return this;
	}
	// 静态方法 地图被单击
	static onMapClickStatic(callback) {
		(new this).onMapClick(callback);
	}
	// 静态方法 地图被双击
	static onMapDBClickStatic(callback) {
		(new this).onMapDBClick(callback);
	}



}
function createDuoMapUni(configs) {
	return new DuoMapUni(configs);
}
export default DuoMapUni;
export {
	createDuoMapUni
}